--[[--
类名: Script.lua
创建日期: 2015-5-12
创建人: 陈小虎
功能描述: 自定义脚本事件解析
修改内容：
修改日期:
修改人:
修改备注:
]]

local Vector = require("app.utils.Vector")

local Event = import(".events.Event")
local TalkEvent = import(".events.TalkEvent")
local HeartWordsEvent = import(".events.HeartWordsEvent")
local StoryModeEvent = import(".events.StoryModeEvent")
local TaskFinishEvent = import(".events.TaskFinishEvent")
local AskForSpecialTask = import(".events.AskForSpecialTask")
local ChangeMapEvent = import(".events.ChangeMapEvent")
local WalkEvent = import(".events.WalkEvent")
local StartEventsEvent = import(".events.StartEventsEvent")
local TaskBattleEvent = import(".events.TaskBattleEvent")
local TaskBattleTillOverEvent = import(".events.TaskBattleTillOverEvent")
local CommonBattleEvent = import(".events.CommonBattleEvent")
local EventsFinishEvent = import(".events.EventsFinishEvent")
local AutoPatrolEvent = import(".events.AutoPatrolEvent")
local AniPlayEvent = import(".events.AniPlayEvent")
local AniMoveEvent = import(".events.AniMoveEvent")
local SoundControlEvent = import(".events.SoundControlEvent")
local DeleteAniEvent = import(".events.DeleteAniEvent")
local PlaySoundEvent = import(".events.PlaySoundEvent")
local StopSoundEvent = import(".events.StopSoundEvent")
local ShowNpcEvent = import(".events.ShowNpcEvent")
local FocusTransEvent = import(".events.FocusTransEvent")
local NpcJumpEvent = import(".events.NpcJumpEvent")
local CameraMoveEvent = import(".events.CameraMoveEvent")
local WaitEvent = import(".events.WaitEvent")
local NpcDirectionEvent = import(".events.NpcDirectionEvent")
local CameraZoomEvent = import(".events.CameraZoomEvent")
local CameraZoomToEvent = import(".events.CameraZoomToEvent")
local NpcMoveEvent = import(".events.NpcMoveEvent")
local RandomEvent = import(".events.RandomEvent")
local LoopRandomEventEvent = import(".events.LoopRandomEventEvent")
local PlayActionEvent = import(".events.PlayActionEvent")
local DeleteNpcEvent = import(".events.DeleteNpcEvent")
local NpcVisibilityEvent = import(".events.NpcVisibilityEvent")
local CameraBackToOrigin = import(".events.CameraBackToOrigin")
local BossCallEvent = import(".events.BossCallEvent")
local MonsterCallEvent = import(".events.MonsterCallEvent")
local ExclamMarkEvent = import(".events.ExclamMarkEvent")
local TaskMarkVisibilityEvent = import(".events.TaskMarkVisibilityEvent")
local NpcFadeEvent = import(".events.NpcFadeEvent")
local SignMarkEvent = import(".events.SignMarkEvent")
local TaskGatherEvent = import(".events.TaskGatherEvent")
local GatherActionEvent = import(".events.GatherActionEvent")
local GatherUICallEvent = import(".events.GatherUICallEvent")
local GatherControlEvent = import(".events.GatherControlEvent")
local GatherToPointEvent = import(".events.GatherToPointEvent")
local SetStoneEvent = import(".events.SetStoneEvent")
local GetOnRideEvent = import(".events.GetOnRideEvent")
local GetOffRideEvent = import(".events.GetOffRideEvent")
local GetRideEvent = import(".events.GetRideEvent")
local NpcFaceToMainEvent = import(".events.NpcFaceToMainEvent")
local MainFaceToNpcEvent = import(".events.MainFaceToNpcEvent")
local JobUnlockEvent = import(".events.JobUnlockEvent")
local TaskLoopUiEvent = import(".events.TaskLoopUiEvent")
local PrestigeDonateUIEvent = import(".events.PrestigeDonateUIEvent")
local PrestigeShopUIEvent = import(".events.PrestigeShopUIEvent")
local BlackScreenEvent = import(".events.BlackScreenEvent")
local FadeScreenEvent = import(".events.FadeScreenEvent")
local MapShakeEvent = import(".events.MapShakeEvent")
local CommonUIEvent = import(".events.CommonUIEvent")
local TypeWordsEvent = import(".events.TypeWordsEvent")
local FadeWordsEvent = import(".events.FadeWordsEvent")
local MapZoomEvent = import(".events.MapZoomEvent")
local SkipOperateEvent = import(".events.SkipOperateEvent")
local CloudEvent = import(".events.CloudEvent")
local TransformEvent = import(".events.TransformEvent")
local ChangeNameEvent = import(".events.ChangeNameEvent")
local HearthStoneEvent = import(".events.HearthStoneEvent")
local DisasterBattleEvent = import(".events.DisasterBattleEvent")
local SkipFlagStartEvent = import(".events.SkipFlagStartEvent")
local SkipFlagEndEvent = import(".events.SkipFlagEndEvent")
local SkipFlagEvent = import(".events.SkipFlagEvent")
local CameraBackToMaxEvent = import(".events.CameraBackToMaxEvent")
local GatherConditionalEvent = import(".events.GatherConditionalEvent")
local DesktopVisibilityEvent = import(".events.DesktopVisibilityEvent")
local StoryBranchEvent = import(".events.StoryBranchEvent")
local CreatePuppetEvent = import(".events.CreatePuppetEvent")
local DeletePuppetEvent = import(".events.DeletePuppetEvent")
local WeatherControlEvent = import(".events.WeatherControlEvent")
local CheckTaskItemEvent = import(".events.CheckTaskItemEvent")
local FunctionLevelUpEvent = import(".events.FunctionLevelUpEvent")
local VideoPlayEvent = import(".events.VideoPlayEvent")
local ReturnLastMapEvent = import(".events.ReturnLastMapEvent")
local MoveToNpcEvent = import(".events.MoveToNpcEvent")
local CountDownEvent = import(".events.CountDownEvent")
local SceneMaskEvent = import(".events.SceneMaskEvent")
local SingleNpcMaskEvent = import(".events.SingleNpcMaskEvent")
local ResetSceneMaskEvent = import(".events.ResetSceneMaskEvent")
local RelativeCameraMoveEvent = import(".events.RelativeCameraMoveEvent")
local EnterDungeonEvent = import(".events.EnterDungeonEvent")
local TransferStatusEvent = import(".events.TransferStatusEvent")
local ShowTvTestingEvent = import(".events.ShowTvTestingEvent")
local TransferInSameMapEvent = import(".events.TransferInSameMapEvent")

local Script = class("Script")


function Script.getNpcById(npcId)
    local npc = NpcManager:getNpcById(npcId)
    return npc
end

function Script.getNpcsById(npcId)
    local npcsTab = {}
    for k,npc in pairs(NpcManager:getNpcVec()) do
        if tonumber(npcId) == tonumber(npc:getId()) then
            table.insert(npcsTab,npc)
        end
    end
    return npcsTab
end

function Script.getEventsVec(eventsTab,destNpcId,taskId,mark)
    if destNpcId then
        local npc = Script.getNpcById(destNpcId)
        if npc then
            npc:setMark(mark)
        end
    end

    local endContentId = 0

    local eventsVec = Vector.new()

    for i,v in ipairs(eventsTab) do
        local syncEventsVec = Vector.new()
        for i1,v1 in ipairs(v) do
            local eventFormat = v1
            if eventFormat.event == Event.TALK_EVENT then
                local talkEvent = TalkEvent.new(eventFormat)
                syncEventsVec:add(talkEvent)
                endContentId = eventFormat.content
            elseif eventFormat.event == Event.HEART_WORDS_EVENT then
                local heartWordsEvent = HeartWordsEvent.new(eventFormat)
                syncEventsVec:add(heartWordsEvent)
            elseif eventFormat.event == Event.STORY_MODE_EVENT then
                eventFormat.npcId = destNpcId
                eventFormat.taskId = taskId
                local storyModeEvent = StoryModeEvent.new(eventFormat)
                syncEventsVec:add(storyModeEvent)
            elseif eventFormat.event == Event.TASK_FINISH_EVENT then
                eventFormat.taskId = taskId
                eventFormat.npcId = destNpcId
                eventFormat.contentId = endContentId
                local taskFinishEvent = TaskFinishEvent.new(eventFormat)
                syncEventsVec:add(taskFinishEvent)
            elseif eventFormat.event == Event.ASK_FOR_SPECIAL_TASK_EVENT then
                eventFormat.npcId = destNpcId
                local askForSpecialTask = AskForSpecialTask.new(eventFormat)
                syncEventsVec:add(askForSpecialTask)
            elseif eventFormat.event == Event.CHANGE_MAP_EVENT then
                local changeMapEvent = ChangeMapEvent.new(eventFormat)
                changeMapEvent.mConfirmNpcId = (changeMapEvent.mConfirmNpcId == nil) and destNpcId or changeMapEvent.mConfirmNpcId
                syncEventsVec:add(changeMapEvent)
            elseif eventFormat.event == Event.WALK_EVENT then
                local walkEvent = WalkEvent.new(eventFormat)
                syncEventsVec:add(walkEvent)
            elseif eventFormat.event == Event.START_EVENTS_EVENT then
                local startEventsEvent = StartEventsEvent.new(eventFormat)
                syncEventsVec:add(startEventsEvent)
            elseif eventFormat.event == Event.TASK_BATTLE_EVENT then
                local taskBattleEvent = TaskBattleEvent.new(eventFormat)
                syncEventsVec:add(taskBattleEvent)
            elseif eventFormat.event == Event.TASK_BATTLE_TILL_OVER_EVENT then
                local taskBattleTillOverEvent = TaskBattleTillOverEvent.new(eventFormat)
                syncEventsVec:add(taskBattleTillOverEvent)
            elseif eventFormat.event == Event.COMMON_BATTLE_EVENT then
                local commonBattleEvent = CommonBattleEvent.new(eventFormat)
                syncEventsVec:add(commonBattleEvent)
            elseif eventFormat.event == Event.AUTO_PATROL_EVENT then
                local autoPatrolEvent = AutoPatrolEvent.new(eventFormat)
                syncEventsVec:add(autoPatrolEvent)
            elseif eventFormat.event == Event.ANI_PLAY_EVENT then
                local aniPlayEvent = AniPlayEvent.new(eventFormat)
                syncEventsVec:add(aniPlayEvent)
            elseif eventFormat.event == Event.ANI_MOVE_EVENT then
                local aniMoveEvent = AniMoveEvent.new(eventFormat)
                syncEventsVec:add(aniMoveEvent)
            elseif eventFormat.event == Event.DELETE_ANI_EVENT then
                local deleteAniEvent = DeleteAniEvent.new(eventFormat)
                syncEventsVec:add(deleteAniEvent)
            elseif eventFormat.event == Event.SOUND_CONTROL_EVENT then
                local soundControlEvent = SoundControlEvent.new(eventFormat)
                syncEventsVec:add(soundControlEvent)
            elseif eventFormat.event == Event.PLAY_SOUND_EVENT then
                local playSoundEvent = PlaySoundEvent.new(eventFormat)
                syncEventsVec:add(playSoundEvent)
            elseif eventFormat.event == Event.STOP_SOUND_EVENT then
                local stopSoundEvent = StopSoundEvent.new(eventFormat)
                syncEventsVec:add(stopSoundEvent)
            elseif eventFormat.event == Event.SHOW_NPC_EVENT then
                local showNpcEvent = ShowNpcEvent.new(eventFormat)
                syncEventsVec:add(showNpcEvent)
            elseif eventFormat.event == Event.FOCUS_TRANS_EVENT then
                local focusTransEvent = FocusTransEvent.new(eventFormat)
                syncEventsVec:add(focusTransEvent)
            elseif eventFormat.event == Event.NPC_JUMP_EVENT then
                local npcJumpEvent = NpcJumpEvent.new(eventFormat)
                syncEventsVec:add(npcJumpEvent)
            elseif eventFormat.event == Event.CAMERA_MOVE_EVENT then
                local cameraMoveEvent = CameraMoveEvent.new(eventFormat)
                syncEventsVec:add(cameraMoveEvent)
            elseif eventFormat.event == Event.WAIT_EVENT then
                local waitEvent = WaitEvent.new(eventFormat)
                syncEventsVec:add(waitEvent)
            elseif eventFormat.event == Event.NPC_DIRECTION_EVENT then
                local npcDirectionEvent = NpcDirectionEvent.new(eventFormat)
                syncEventsVec:add(npcDirectionEvent)
            elseif eventFormat.event == Event.CAMERA_ZOOM_EVENT then
                local cameraZoomEvent = CameraZoomEvent.new(eventFormat)
                syncEventsVec:add(cameraZoomEvent)
            elseif eventFormat.event == Event.CAMERA_ZOOM_TO_EVENT then
                local cameraZoomToEvent = CameraZoomToEvent.new(eventFormat)
                syncEventsVec:add(cameraZoomToEvent)
            elseif eventFormat.event == Event.NPC_MOVE_EVENT then
                local npcMoveEvent = NpcMoveEvent.new(eventFormat)
                syncEventsVec:add(npcMoveEvent)
            elseif eventFormat.event == Event.RANDOM_EVENT then
                local randomEvent = RandomEvent.new(eventFormat)
                for k,v in pairs(eventFormat.events) do
                    local eventsTab1 = v
--                     print("---------- Script.getEventsVec")
--                     dump(eventsTab1)
                    local sEventsVec = Script.getEventsVec(eventsTab1,destNpcId,taskId)
                    local syncEventsVec = Vector.new()
                    local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = false})
                    syncEventsVec:add(signMarkEvent)
                    sEventsVec:setElementAt(syncEventsVec,1)

                    local syncEventsVec1 = Vector.new()
                    local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                    syncEventsVec1:add(signMarkEvent1)
                    sEventsVec:add(syncEventsVec1)

                    randomEvent.mEventsVec:add(sEventsVec)
                end
                syncEventsVec:add(randomEvent)
            elseif eventFormat.event == Event.LOOP_RANDOM_EVENT_EVENT then
                --local loopRandomEventEvent = LoopRandomEventEvent.new(eventFormat)
            elseif eventFormat.event == Event.PLAY_ACTION_EVENT then
                local playActionEvent = PlayActionEvent.new(eventFormat)
                syncEventsVec:add(playActionEvent)
            elseif eventFormat.event == Event.DELETE_NPC_EVENT then
                local deleteNpcEvent = DeleteNpcEvent.new(eventFormat)
                syncEventsVec:add(deleteNpcEvent)
            elseif eventFormat.event == Event.NPC_VISIBILITY_EVENT then
                local npcVisibilityEvent = NpcVisibilityEvent.new(eventFormat)
                syncEventsVec:add(npcVisibilityEvent)
            elseif eventFormat.event == Event.CAMERA_BACKTO_ORIGIN then
                local cameraBackToOrigin = CameraBackToOrigin.new(eventFormat)
                syncEventsVec:add(cameraBackToOrigin)
            elseif eventFormat.event == Event.MONSTER_CALL_EVENT then
                local monsterCallEvent = MonsterCallEvent.new(eventFormat)

                local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)
                local cancelEventsVec = Script.getEventsVec(eventFormat.cancel,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                okEventsVec:add(syncEventsVec1)

                local syncEventsVec2 = Vector.new()
                local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec2:add(signMarkEvent2)
                cancelEventsVec:add(syncEventsVec2)

                monsterCallEvent.mOkEventsVec = okEventsVec
                monsterCallEvent.mCancelEventsVec = cancelEventsVec

                syncEventsVec:add(monsterCallEvent)
            elseif eventFormat.event == Event.EXCLAM_MARK_EVENT then
                local exclamMarkEvent = ExclamMarkEvent.new(eventFormat)
                syncEventsVec:add(exclamMarkEvent)
            elseif eventFormat.event == Event.TASK_MARK_VISIBILITY_EVENT then
                local taskMarkVisibilityEvent = TaskMarkVisibilityEvent.new(eventFormat)
                syncEventsVec:add(taskMarkVisibilityEvent)
            elseif eventFormat.event == Event.NPC_FADE_EVENT then
                local npcFadeEvent = NpcFadeEvent.new(eventFormat)
                syncEventsVec:add(npcFadeEvent)
            elseif eventFormat.event == Event.SIGN_MARK_EVENT then
                local signMarkEvent = SignMarkEvent.new(eventFormat)
                syncEventsVec:add(signMarkEvent)
            elseif eventFormat.event == Event.TASK_GATHER_EVENT then
                local taskGatherEvent = TaskGatherEvent.new(eventFormat)
                syncEventsVec:add(taskGatherEvent)
            elseif eventFormat.event == Event.GATHER_ACTION_EVENT then
                local gatherActionEvent = GatherActionEvent.new(eventFormat)
                syncEventsVec:add(gatherActionEvent)
            elseif eventFormat.event == Event.GATHER_UI_CALL_EVENT then
                local gatherUICallEvent = GatherUICallEvent.new(eventFormat)

                local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)
                local cancelEventsVec = Script.getEventsVec(eventFormat.cancel,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                okEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- okEventsVec:add(removeSkipEventsVec)

                local syncEventsVec2 = Vector.new()
                local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec2:add(signMarkEvent2)
                cancelEventsVec:add(syncEventsVec2)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- cancelEventsVec:add(removeSkipEventsVec)

                gatherUICallEvent.mOkEventsVec = okEventsVec
                gatherUICallEvent.mCancelEventsVec = cancelEventsVec

                syncEventsVec:add(gatherUICallEvent)
            elseif eventFormat.event == Event.GATHER_CONTROL_EVENT then
                local gatherControlEvent = GatherControlEvent.new(eventFormat)
                syncEventsVec:add(gatherControlEvent)
            elseif eventFormat.event == Event.GATHER_TO_POINT_EVENT then
                local gatherToPointEvent = GatherToPointEvent.new(eventFormat)
                syncEventsVec:add(gatherToPointEvent)
            elseif eventFormat.event == Event.SET_STONE_EVENT then
                local setStoneEvent = SetStoneEvent.new(eventFormat)

                local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)
                local cancelEventsVec = Script.getEventsVec(eventFormat.cancel,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                okEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- okEventsVec:add(removeSkipEventsVec)

                local syncEventsVec2 = Vector.new()
                local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec2:add(signMarkEvent2)
                cancelEventsVec:add(syncEventsVec2)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- cancelEventsVec:add(removeSkipEventsVec)

                setStoneEvent.mOkEventsVec = okEventsVec
                setStoneEvent.mCancelEventsVec = cancelEventsVec

                syncEventsVec:add(setStoneEvent)
            elseif eventFormat.event == Event.GET_ON_RIDE_EVENT then
                local getOnRideEvent = GetOnRideEvent.new(eventFormat)
                syncEventsVec:add(getOnRideEvent)
            elseif eventFormat.event == Event.GET_OFF_RIDE_EVENT then
                local getOffRideEvent = GetOffRideEvent.new(eventFormat)
                syncEventsVec:add(getOffRideEvent)
            elseif eventFormat.event == Event.GET_RIDE_EVENT then
                local getRideEvent = GetRideEvent.new(eventFormat)
                syncEventsVec:add(getRideEvent)
            elseif eventFormat.event == Event.NPC_FACETO_MAIN_EVENT then
                local npcFaceToMainEvent = NpcFaceToMainEvent.new(eventFormat)
                syncEventsVec:add(npcFaceToMainEvent)
            elseif eventFormat.event == Event.MAIN_FACETO_NPC_EVENT then
                local mainFaceToNpcEvent = MainFaceToNpcEvent.new(eventFormat)
                syncEventsVec:add(mainFaceToNpcEvent)
            elseif eventFormat.event == Event.JOB_UNLOCK_EVENT then
                local jobUnlockEvent = JobUnlockEvent.new(eventFormat)

                local returnEventsVec = Script.getEventsVec(eventFormat.rt,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                returnEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- returnEventsVec:add(removeSkipEventsVec)

                jobUnlockEvent.mReturnEventsVec = returnEventsVec

                syncEventsVec:add(jobUnlockEvent)
            elseif eventFormat.event == Event.TASK_LOOP_UI_EVENT then
                local taskLoopUiEvent = TaskLoopUiEvent.new(eventFormat)

                local returnEventsVec = Script.getEventsVec(eventFormat.rt,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                returnEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- returnEventsVec:add(removeSkipEventsVec)

                taskLoopUiEvent.mReturnEventsVec = returnEventsVec

                syncEventsVec:add(taskLoopUiEvent)
            elseif eventFormat.event == Event.PRESTIGE_DONATE_UI_EVENT then
                local prestigeDonateUIEvent = PrestigeDonateUIEvent.new(eventFormat)

                local returnEventsVec = Script.getEventsVec(eventFormat.rt,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                returnEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- returnEventsVec:add(removeSkipEventsVec)

                prestigeDonateUIEvent.mReturnEventsVec = returnEventsVec

                syncEventsVec:add(prestigeDonateUIEvent)
            elseif eventFormat.event == Event.PRESTIGE_SHOP_UI_EVENT then
                local prestigeShopUIEvent = PrestigeShopUIEvent.new(eventFormat)

                local returnEventsVec = Script.getEventsVec(eventFormat.rt,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                returnEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- returnEventsVec:add(removeSkipEventsVec)

                prestigeShopUIEvent.mReturnEventsVec = returnEventsVec

                syncEventsVec:add(prestigeShopUIEvent)
            elseif eventFormat.event == Event.BLACK_SCREEN_EVENT then
                local blackScreenEvent = BlackScreenEvent.new(eventFormat)
                syncEventsVec:add(blackScreenEvent)
            elseif eventFormat.event == Event.FADE_SCREEN_EVENT then
                local fadeScreenEvent = FadeScreenEvent.new(eventFormat)
                syncEventsVec:add(fadeScreenEvent)
            elseif eventFormat.event == Event.MAP_SHAKE_EVENT then
                local mapShakeEvent = MapShakeEvent.new(eventFormat)
                syncEventsVec:add(mapShakeEvent)
            elseif eventFormat.event == Event.COMMON_UI_EVENT then
                local commonUiEvent = CommonUIEvent.new(eventFormat)

                local returnEventsVec = Script.getEventsVec(eventFormat.rt,destNpcId,taskId)

                local syncEventsVec1 = Vector.new()
                local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                syncEventsVec1:add(signMarkEvent1)
                returnEventsVec:add(syncEventsVec1)

                -- local removeSkipEventsVec = Script.getRemoveSkipEventsVec()
                -- returnEventsVec:add(removeSkipEventsVec)

                commonUiEvent.mReturnEventsVec = returnEventsVec
                if eventFormat.ok then
                    local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)

                    local syncEventsVec2 = Vector.new()
                    local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
                    syncEventsVec2:add(signMarkEvent2)
                    okEventsVec:add(syncEventsVec2)
                    commonUiEvent.mOkEventsVec = okEventsVec
                end

                syncEventsVec:add(commonUiEvent)
            end
            Script.createEvents(eventFormat,eventsVec,syncEventsVec,destNpcId,taskId,mark)
        end
        eventsVec:add(syncEventsVec)
    end

    return eventsVec
end

function Script.createEvents(eventFormat,eventsVec,syncEventsVec,destNpcId,taskId,mark)
    if eventFormat.event == Event.TYPE_WORDS_EVENT then
        local typeWordsEvent = TypeWordsEvent.new(eventFormat)
        syncEventsVec:add(typeWordsEvent)
    elseif eventFormat.event == Event.FADE_WORDS_EVENT then
        local fadeWordsEvent = FadeWordsEvent.new(eventFormat)
        syncEventsVec:add(fadeWordsEvent)
    elseif eventFormat.event == Event.MAP_ZOOM_EVENT then
        local mapZoomEvent = MapZoomEvent.new(eventFormat)
        syncEventsVec:add(mapZoomEvent)
    elseif eventFormat.event == Event.SKIP_OPERATE_EVENT then
        local skipOperateEvent = SkipOperateEvent.new(eventFormat)
        syncEventsVec:add(skipOperateEvent)
    elseif eventFormat.event == Event.CLOUD_EVENT then
        local coundEvent = CloudEvent.new(eventFormat)
        syncEventsVec:add(coundEvent)
    elseif eventFormat.event == Event.TRANSFORM_EVENT then
        local transformEvent = TransformEvent.new(eventFormat)
        syncEventsVec:add(transformEvent)
    elseif eventFormat.event == Event.HEARTH_STONE_EVENT then
        local hearthStoneEvent = HearthStoneEvent.new(eventFormat)
        syncEventsVec:add(hearthStoneEvent)
    elseif eventFormat.event == Event.DISASTER_BATTLE_EVENT then
        local disasterBattleEvent = DisasterBattleEvent.new(eventFormat)
        syncEventsVec:add(disasterBattleEvent)
    elseif eventFormat.event == Event.SKIP_FLAG_START_EVENT then
        eventFormat.taskId = taskId
        local skipFlagStartEvent = SkipFlagStartEvent.new(eventFormat)
        syncEventsVec:add(skipFlagStartEvent)
    elseif eventFormat.event == Event.SKIP_FLAG_END_EVENT then
        local skipFlagEndEvent = SkipFlagEndEvent.new(eventFormat)
        syncEventsVec:add(skipFlagEndEvent)
    elseif eventFormat.event == Event.SKIP_FLAG_EVENT then
        eventFormat.taskId = taskId
        local skipFlagEvent = SkipFlagEvent.new(eventFormat)
        syncEventsVec:add(skipFlagEvent)
    elseif eventFormat.event == Event.CAMERA_BACKTO_MAX_EVENT then
        local cameraBackToMaxEvent = CameraBackToMaxEvent.new(eventFormat)
        syncEventsVec:add(cameraBackToMaxEvent)
    elseif eventFormat.event == Event.GATHER_CONDITIONAL_EVENT then
        local gatherConditionalEvent = GatherConditionalEvent.new(eventFormat)
        syncEventsVec:add(gatherConditionalEvent)
    elseif eventFormat.event == Event.DESKTOP_VISIBILITY_EVENT then
        local desktopVisibilityEvent = DesktopVisibilityEvent.new(eventFormat)
        syncEventsVec:add(desktopVisibilityEvent)
    elseif eventFormat.event == Event.STORY_BRANCH_EVENT then
        local storyBranchEvent = StoryBranchEvent.new(eventFormat)

        local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)
        local cancelEventsVec = Script.getEventsVec(eventFormat.cancel,destNpcId,taskId)

        local syncEventsVec1 = Vector.new()
        local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
        syncEventsVec1:add(signMarkEvent1)
        okEventsVec:add(syncEventsVec1)

        local syncEventsVec2 = Vector.new()
        local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
        syncEventsVec2:add(signMarkEvent2)
        cancelEventsVec:add(syncEventsVec2)

        storyBranchEvent.mOkEventsVec = okEventsVec
        storyBranchEvent.mCancelEventsVec = cancelEventsVec

        syncEventsVec:add(storyBranchEvent)
    elseif eventFormat.event == Event.CREATE_PUPPET_EVENT then
        local createPuppetEvent = CreatePuppetEvent.new(eventFormat)
        syncEventsVec:add(createPuppetEvent)
    elseif eventFormat.event == Event.DELETE_PUPPET_EVENT then
        local deletePuppetEvent = DeletePuppetEvent.new(eventFormat)
        syncEventsVec:add(deletePuppetEvent)
    elseif eventFormat.event == Event.WEATHER_CONTROL_EVENT then
        local weatherControlEvent = WeatherControlEvent.new(eventFormat)
        syncEventsVec:add(weatherControlEvent)
    elseif eventFormat.event == Event.CHECK_TASK_ITEM_EVENT then
        eventFormat.taskId = taskId
        local checkTaskItemEvent = CheckTaskItemEvent.new(eventFormat)

        local okEventsVec = Script.getEventsVec(eventFormat.ok,destNpcId,taskId)
        local okNotEnoughEventsVec
        if eventFormat.ok_notEnough then
            okNotEnoughEventsVec = Script.getEventsVec(eventFormat.ok_notEnough,destNpcId,taskId)
        end
        local cancelEventsVec = Script.getEventsVec(eventFormat.cancel,destNpcId,taskId)

        local syncEventsVec1 = Vector.new()
        local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
        syncEventsVec1:add(signMarkEvent1)
        okEventsVec:add(syncEventsVec1)

        local syncEventsVec2 = Vector.new()
        local signMarkEvent2 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
        syncEventsVec2:add(signMarkEvent2)
        cancelEventsVec:add(syncEventsVec2)

        if okNotEnoughEventsVec then
            local syncEventsVec3 = Vector.new()
            local signMarkEvent3 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
            syncEventsVec3:add(signMarkEvent3)
            okNotEnoughEventsVec:add(syncEventsVec3)
            checkTaskItemEvent.mOkNotEnoughEventsVec = okNotEnoughEventsVec
        end


        checkTaskItemEvent.mOkEventsVec = okEventsVec
        checkTaskItemEvent.mCancelEventsVec = cancelEventsVec

        syncEventsVec:add(checkTaskItemEvent)
    elseif eventFormat.event == Event.FUNCTION_LEVELUP_EVENT then
        local functionLevelUpEvent = FunctionLevelUpEvent.new(eventFormat)
        syncEventsVec:add(functionLevelUpEvent)
    elseif eventFormat.event == Event.VIDEO_PLAY_EVENT then
        local videoPlayEvent = VideoPlayEvent.new(eventFormat)
        syncEventsVec:add(videoPlayEvent)
    elseif eventFormat.event == Event.MOVE_TO_NPC_EVENT then
        local moveToNpcEvent = MoveToNpcEvent.new(eventFormat)
        syncEventsVec:add(moveToNpcEvent)
    elseif eventFormat.event == Event.COUNT_DOWN_EVENT then
        local countDownEvet = CountDownEvent.new(eventFormat)
        syncEventsVec:add(countDownEvet)
    elseif eventFormat.event == Event.SCENE_MASK_EVENT then
        local sceneMaskEvent = SceneMaskEvent.new(eventFormat)
        syncEventsVec:add(sceneMaskEvent)
    elseif eventFormat.event == Event.SINGLE_NPC_MASK_EVENT then
        local singleNpcMaskEvent = SingleNpcMaskEvent.new(eventFormat)
        syncEventsVec:add(singleNpcMaskEvent)
    elseif eventFormat.event == Event.RESET_SCENE_MASK_EVENT then
        local resetSceneMaskEvent = ResetSceneMaskEvent.new(eventFormat)
        syncEventsVec:add(resetSceneMaskEvent)
    elseif eventFormat.event == Event.PRE_BATTLE_EVENT then
        Script.createPreBattleEvent(eventsVec,eventFormat,destNpcId)
    elseif eventFormat.event == Event.RELATIVE_CAMERA_MOVE_EVENT then
        local relativeCameraMoveEvent = RelativeCameraMoveEvent.new(eventFormat)
        syncEventsVec:add(relativeCameraMoveEvent)
    elseif eventFormat.event == Event.ENTER_DUNGEON_EVENT then
        local enterDungeonEvent = EnterDungeonEvent.new(eventFormat)
        syncEventsVec:add(enterDungeonEvent)
    elseif eventFormat.event == Event.TRANSFER_STATUS_EVENT then
        local transferStatusEvent = TransferStatusEvent.new(eventFormat)
        syncEventsVec:add(transferStatusEvent)
    elseif eventFormat.event == Event.SHOW_TV_TESTING_EVENT then
        local showTvTestingEvent = ShowTvTestingEvent.new(eventFormat)
        syncEventsVec:add(showTvTestingEvent)
    elseif eventFormat.event == Event.TRANSFER_IN_SAME_MAP_EVENT then
        local transfreInSameMapEvent = TransferInSameMapEvent.new(eventFormat)
        syncEventsVec:add(transfreInSameMapEvent)
    end
end

function Script.createPreBattleEvent(eventsVec,eventFormat,npcId)
    local vec = Script.loadPreBattleScript(npcId,eventFormat.flag)
    if not vec then return end
    for i = 1,vec:size() do
        local sVec = vec:get(i)
        eventsVec:add(sVec)
    end
end

function Script.getDefaultSyncEventsVec()
    local cameraBackToOrigin = CameraBackToOrigin.new({event = "CAMERA_BACKTO_ORIGIN",time = 0.5})
    local syncEventsVec = Vector.new()
    syncEventsVec:add(cameraBackToOrigin)

    return syncEventsVec
end

function Script.getShowSkipEventsVec()
    local skipOperateEvent = SkipOperateEvent.new({event = "SKIP_OPERATE_EVENT",show = true})
    local syncEventsVec = Vector.new()
    syncEventsVec:add(skipOperateEvent)

    return syncEventsVec
end

function Script.getRemoveSkipEventsVec()
    local skipOperateEvent = SkipOperateEvent.new({event = "SKIP_OPERATE_EVENT",show = false})
    local syncEventsVec = Vector.new()
    syncEventsVec:add(skipOperateEvent)

    return syncEventsVec
end

function Script.parseInitEvents(eventsTab)
    if not eventsTab then
        return
    end



end

function Script.parseClickEvents(script)
    if not script then
        return
    end

    local function parseEvents(npc,eventsTab,destNpcId,taskId,mark)
        if npc then
            local eventsVec = Script.getEventsVec(eventsTab,destNpcId,taskId,mark)
            local syncEventsVec = Vector.new()
            local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
            syncEventsVec:add(signMarkEvent)
            eventsVec:add(syncEventsVec)

            npc:addEventsAt(1,eventsVec)
        end
    end

    for k,v in pairs(script) do
        local eventsTab = v
        if eventsTab and eventsTab.eventNpcId then

            local destNpcId = eventsTab.eventNpcId
            local taskId = eventsTab.taskId
            local mark = eventsTab.mark

            local npc = Script.getNpcById(destNpcId)
            if npc then

                local tag = 0
                local sayStr = ""
                if eventsTab.condition then
                    local field = eventsTab.condition.field
                    if field then
                        -- do nothing
                    end
                    local sayFlag = eventsTab.condition.string
                    if sayFlag then
                        sayStr = L(sayFlag)
                    end
                end

                npc:setSingleParams({tag = tag,say = sayStr})

                parseEvents(npc,eventsTab,destNpcId,taskId,mark)
            end
        end
    end
end

function Script.parseAreaEvents(eventsTab,area)
    if not eventsTab or not area then
        return
    end

    local destNpcId = eventsTab.eventNpcId
    local taskId = eventsTab.taskId
    local mark = eventsTab.mark

    local eventsVec = Script.getEventsVec(eventsTab,destNpcId,taskId,mark)
    area:addEventsAt(1,eventsVec)
end

function Script.parseTaskEvents(eventsTab)
    if not eventsTab then return end

    local ox = 0
    if eventsTab.anchorPos then
        ox = tonumber(eventsTab.anchorPos)
    elseif eventsTab.anchorNpc then
        local npcId = tonumber(eventsTab.anchorNpc)
        if npcId == 0 then
            ox = FieldManager:getCharacterX()
        else
            local npc = NpcManager:getNpcById(npcId)
            if npc then
                ox = npc:getPositionX()
            else
                local npcC = NpcManager:getNpcConfigById(npcId)
                if npcC then
                    ox = tonumber(npcC.coordinate) or 0
                end
            end
        end
    end

    local mapId = eventsTab.mapid
    local taskId = eventsTab.taskId
    local eventsVec = Script.getEventsVec(eventsTab,nil,taskId)

    return {mapId = mapId,taskId = taskId,ox = ox,anchorNpc = eventsTab.anchorNpc,events = eventsVec}
end

function Script.parseCommonTaskEvents(eventsTab,npcId,npcO)
    if not eventsTab then return end
    local npc = (npcO == nil) and Script.getNpcById(npcId) or npcO
    if not npc then return end

    local talkEventsVec = Vector.new()
    local commonEventsVec = nil
    if eventsTab["common"] then
        commonEventsVec = Script.getEventsVec(eventsTab["common"],npcId)
    end
    for k,v in pairs(eventsTab) do
        if k == "main" then
            local maxTaskId = 0
            for k1,v1 in pairs(v) do
                local taskId = tonumber(k1)
                if maxTaskId < taskId and taskId < UD:getCurTaskId() then
                    maxTaskId = taskId
                end
            end
            if maxTaskId ~= 0 then
                local eventsVec = Script.getEventsVec(v[maxTaskId],npcId,maxTaskId)
                for i = 1,eventsVec:size() do
                    local sVec = eventsVec:get(i)
                    talkEventsVec:add(sVec)
                end
            end

        elseif k == "side" then
            for k2,v2 in pairs(v) do
                if UD:isTaskFinished(tonumber(k2)) then
                    local eventsVec = Script.getEventsVec(v2,npcId,tonumber(k2))
                    for i = 1,eventsVec:size() do
                        local sVec = eventsVec:get(i)
                        talkEventsVec:add(sVec)
                    end
                end
            end
        end
    end

    if commonEventsVec and commonEventsVec:size() > 0 then
        for i = commonEventsVec:size(), 1, -1 do
            local sVec = commonEventsVec:get(i)
            talkEventsVec:setElementAt(sVec,1)
        end
    end

    if talkEventsVec:size() > 0 then
        local syncEventsVec = Vector.new()
        local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = mark,show = true})
        syncEventsVec:add(signMarkEvent)
        talkEventsVec:add(syncEventsVec)

        local syncEventsVec1 = Vector.new()
        local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = npcId})
        syncEventsVec1:add(eventsFinishEvent)
        talkEventsVec:add(syncEventsVec1)

        npc:setEvents("talk",talkEventsVec)
    end
end

function Script.parseCommonClickEvents(eventsTab,curTaskId,npcO)
    if not eventsTab then return end

    local function getEvents(eventsTab,destNpcId,mark)
        local eventsVec = Script.getEventsVec(eventsTab,destNpcId,nil,mark)

        local syncEventsVec = Vector.new()
        local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = destNpcId,mark = mark,show = true})
        syncEventsVec:add(signMarkEvent)
        eventsVec:add(syncEventsVec)

        local syncEventsVec1 = Vector.new()
        local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = destNpcId})
        syncEventsVec1:add(eventsFinishEvent)
        eventsVec:add(syncEventsVec1)
        return eventsVec
    end


    local destNpcId = eventsTab.eventNpcId
    --print("Script.parseCommonClickEvents destNpcId = ",destNpcId)
    local mark = eventsTab.mark
    local takeTaskId = eventsTab.takeTaskId

    if takeTaskId ~= nil then
        if curTaskId < takeTaskId then
            return
        end
    end

    local npc = (npcO == nil) and Script.getNpcById(destNpcId) or npcO
    if not npc then return end
    --dump(eventsTab)

    local eventsVec = getEvents(eventsTab,destNpcId,mark)

   -- for i = 1, eventsVec:size() do
   --     local syncVec = eventsVec:get(i)
   --     for j = 1,syncVec:size() do
   --         local event = syncVec:get(j)
   --         print("Script.parseCommonClickEvents event.mEventName",event.mEventName)
   --     end
   -- end
--
--    print("Script.parseCommonClickEvents len = ",#eventsVec)

    -- npc:addEvents(eventsVec)
    npc:setEvents("talk",eventsVec)
end

function Script.parseCommonConfigTalks(talk,randTalk,npcId,curTaskId,npcO)

    if talk then
        local mutiTaskTalks = string.split(talk,";")
        for i,v in ipairs(mutiTaskTalks) do--多个task
            -- print("Script.parseCommonConfigTalks  i = ",i)
            -- print("Script.parseCommonConfigTalks  v = ",v)
            local taskTalk = string.split(v,"@")
            local taskId = taskTalk[1]
            local talksString = taskTalk[2]
            local talks = string.split(talksString,"#")
            -- print("Script.parseCommonConfigTalks  taskId = ",taskId)
            -- print("Script.parseCommonConfigTalks  curTaskId = ",curTaskId)

            if tonumber(curTaskId) >= tonumber(taskId) then
                local eventsVec = Vector.new()

                for i1,v1 in ipairs(talks) do--一个task的多句话
                    -- print("Script.parseCommonConfigTalks  i1 = ",i1)
                    local talkParams = string.split(v1,"|")

                    -- print("Script.parseCommonConfigTalks  talkParams = ",talkParams)
                    local string = talkParams[1]
                    local time = talkParams[2]
                    local action = talkParams[3]
                    local sound = talkParams[4]
                    -- print("Script.parseCommonConfigTalks  string = ",string)
                    -- print("Script.parseCommonConfigTalks  time = ",time)
                    -- print("Script.parseCommonConfigTalks  action = ",action)
                    -- print("Script.parseCommonConfigTalks  sound = ",sound)

                    local talkEvent = TalkEvent.new({event = "TALK_EVENT",npcId = npcId,string = string,time = time,sound = sound,action = action})
                    local syncEventsVec = Vector.new()
                    syncEventsVec:add(talkEvent)
                    eventsVec:add(syncEventsVec)
                end
                local syncEventsVec = Vector.new()
                local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = "commonTalk",show = true})
                syncEventsVec:add(signMarkEvent)
                eventsVec:add(syncEventsVec)

                local syncEventsVec1 = Vector.new()
                local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = npcId})
                syncEventsVec1:add(eventsFinishEvent)
                eventsVec:add(syncEventsVec1)

                local npc = (npcO == nil) and Script.getNpcById(npcId) or npcO
                if npc then
                    -- npc:addEventsAt(1,eventsVec)
                    npc:setEvents("talk",eventsVec)
                end
            end
        end
    end
    if randTalk then
        local mutiRandTaskTalks = string.split(randTalk,";")
        for i,v in ipairs(mutiRandTaskTalks) do--多个task
            local taskTalk = string.split(v,"@")
            local task = string.split(taskTalk[1],"|")
            local taskId = tonumber(task[1])
            local rand = task[2]
            -- local talks = string.split(talksString,"#")
            if tonumber(curTaskId) >= taskId and rand == "rand" then
                local eventsTab = {}
                local talkArrays = string.split(taskTalk[2],"&")
                for i1,v1 in ipairs(talkArrays) do--多段

                    local talkEvents = {}
                    local talks = string.split(v1,"#")
                    for i2,v2 in ipairs(talks) do--多句
                        local talkParams = string.split(v2,"|")

                        local string = talkParams[1]
                        local time = talkParams[2]
                        local action = talkParams[3]
                        local sound = talkParams[4]

                        local talkEvent = {{event = "TALK_EVENT",npcId = npcId,string = string,time = time,sound = sound,action = action}}
                        table.insert(talkEvents,talkEvent)
                    end
                    table.insert(eventsTab,talkEvents)
                end

                local randomEvent = RandomEvent.new({event = "RANDOM_EVENT"})
                for k,v in pairs(eventsTab) do
                    local eventsTab1 = v
                    local sEventsVec = Script.getEventsVec(eventsTab1,npcId,taskId,"commonTalk")

                    local syncEventsVec = Vector.new()
                    local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = "commonTalk",show = false})
                    syncEventsVec:add(signMarkEvent)
                    sEventsVec:setElementAt(syncEventsVec,1)

                    local syncEventsVec1 = Vector.new()
                    local signMarkEvent1 = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = "commonTalk",show = true})
                    syncEventsVec1:add(signMarkEvent1)
                    sEventsVec:add(syncEventsVec1)

                    randomEvent.mEventsVec:add(sEventsVec)
                end
                local syncEventsVec = Vector.new()
                syncEventsVec:add(randomEvent)

                local eventsVec = Vector.new()
                eventsVec:add(syncEventsVec)

                local syncEventsVec = Vector.new()
                local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = "commonTalk",show = true})
                syncEventsVec:add(signMarkEvent)
                eventsVec:add(syncEventsVec)

                local syncEventsVec1 = Vector.new()
                local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = npcId})
                syncEventsVec1:add(eventsFinishEvent)
                eventsVec:add(syncEventsVec1)

                local npc = (npcO == nil) and Script.getNpcById(npcId) or npcO
                if npc then
                     npc:setEvents("talk",eventsVec)
                end
            end
        end
    end
end

function Script.parseEscortEvents(eventsTab,npcO)
    if not eventsTab then return end

    local destNpcId = eventsTab.eventNpcId

    local npc = (npcO == nil) and Script.getNpcById(destNpcId) or npcO
    if not npc then return end

    for i,v in ipairs(eventsTab) do
        local posx = v.posx
        local triggerDir = v.triggerDir
        local events = v.events

        local eventsVec = Script.getEventsVec(events,destNpcId)

        local syncEventsVec = Vector.new()
        local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = destNpcId})
        syncEventsVec:add(eventsFinishEvent)
        eventsVec:add(syncEventsVec)

        local eScripts = {
            posx = posx,
            triggerDir = triggerDir,
            play = false,
            events = eventsVec
        }

        npc:addEscortEvents(eScripts)
    end
end

function Script.parseRandomEvents(events)

end

function Script.loadFunctionLevelUpScript(scriptName)
    if not scriptName then return end
    local eventsVec = nil
    local path = "scripts.commonscripts." .. scriptName
    local script = app:loadResScript(path)
    if script then
        script.init()
        eventsVec = Script.getEventsVec(script.events)
        script.events = {}
    end
    return eventsVec
end

function Script.loadPreBattleScript(npcId,flag)
    local eventsVec = nil
    local path = "scripts.prebattle.PreBattle"
    local script = app:loadResScript(path)
    if script then
        local name = "pre_" .. flag
        script.init({npcId = npcId})
        if script[name] then
            eventsVec = Script.getEventsVec(script[name],npcId)
            script[name] = {}
        end
    end
    return eventsVec
end

function Script.loadDungeonScript(npcConfig)
    if not npcConfig
        or npcConfig.dialogScript == nil
        or tonumber(npcConfig.npcCategories) ~= Enums.NpcVariety.dungeon
        then return
    end
    local path = "scripts.dungeon." .. npcConfig.dialogScript

    local script = app:loadResScript(path)
    if script then
        local npcId = tonumber(npcConfig.ID)
        if script.areaEvents then
            -- local events = Script.getEventsVec(script.areaEvents,npcId)
            local area = NpcManager:getArea(tonumber(npcId))
            if area then
                Script.parseAreaEvents(script.areaEvents,area)
            end
        elseif script.clickEvents then
            -- local events = Script.getEventsVec(script.clickEvents,npcId)
            local npc = Script.getNpcById(npcId)
            if npc then
                Script.parseClickEvents(script)
            end
        elseif script.autoEvents then
            local evetns = Script.getEventsVec(script.autoEvents,npcId)
            local npc = Script.getNpcById(npcId)
            if npc then
                npc:addDungeonEvents(evetns,true)
                npc:setDungeonNpcSwitch(true)
            end
        end
    end
end

function Script.getFinishEvent(npcId,eventsVec)
    local syncEventsVec = Vector.new()
    local signMarkEvent = SignMarkEvent.new({event = "SIGN_MARK_EVENT",npcId = npcId,mark = nil,show = true})
    syncEventsVec:add(signMarkEvent)
    eventsVec:add(syncEventsVec)

    syncEventsVec = Vector.new()
    local eventsFinishEvent = EventsFinishEvent.new({event = "EVENTS_FINISH_EVENT",npcId = npcId})
    syncEventsVec:add(eventsFinishEvent)
    eventsVec:add(syncEventsVec)
    return syncEventsVec
end

function Script.createSyncEvents(events)
    local syncEventsVec = Vector.new()
    for i,v in ipairs(events) do
        local event = v
        syncEventsVec:add(event)
    end
    return syncEventsVec
end

function Script.createTaskGatherEvents(npcId, npcUid)
    -- local gatherConditionalEvent = GatherConditionalEvent.new({event = "GATHER_CONDITIONAL_EVENT",npcId = npcId,todo = "chests"})
    local gatherToPointEvent = GatherToPointEvent.new({event = "GATHER_TO_POINT_EVENT",npcId = npcId, npcUid = npcUid})
    local getOffRideEvent = GetOffRideEvent.new({event = "GET_OFF_RIDE_EVENT"})
    -- local gatherActionEvent = GatherActionEvent.new({event = "GATHER_ACTION_EVENT",todo = "chests",gatherNpcId = npcId,isTask = true})
    local taskGatherEvent = TaskGatherEvent.new({event = "TASK_GATHER_EVENT",gatherNpcId = npcId, npcUid = npcUid})
    local gatherControlEvent = GatherControlEvent.new({event = "GATHER_CONTROL_EVENT"})

    local events = Vector.new()
    -- local syncEvents = Script.createSyncEvents({gatherConditionalEvent})
    -- events:add(syncEvents)
    local syncEvents = Script.createSyncEvents({gatherToPointEvent})
    events:add(syncEvents)
    syncEvents = Script.createSyncEvents({getOffRideEvent,taskGatherEvent,gatherControlEvent})
    events:add(syncEvents)
    return events
end

function Script.createSwitchMapEvents(mapId, transNpcId)

    local transferStatusEvent = TransferStatusEvent.new({event = "TRANSFER_STATUS_EVENT",start = true})
    local desktopVisibilityEvent = DesktopVisibilityEvent.new({event = "DESKTOP_VISIBILITY_EVENT",visible = false})
    local hearthStoneEvent = HearthStoneEvent.new({event = "HEARTH_STONE_EVENT"})
    local transferStatusEvent1 = TransferStatusEvent.new({event = "TRANSFER_STATUS_EVENT",start = false})
    local changeMapEvent = ChangeMapEvent.new({event = "CHANGE_MAP_EVENT",mapId = mapId, confirmNpcId = transNpcId,})

    local events = Vector.new()
    local syncEvents = Script.createSyncEvents({transferStatusEvent})
    events:add(syncEvents)
    syncEvents = Script.createSyncEvents({desktopVisibilityEvent})
    events:add(syncEvents)
    syncEvents = Script.createSyncEvents({hearthStoneEvent})
    events:add(syncEvents)
    syncEvents = Script.createSyncEvents({transferStatusEvent1})
    events:add(syncEvents)
    syncEvents = Script.createSyncEvents({changeMapEvent})
    events:add(syncEvents)
    return events
end

return Script
